//
//  RunTimeVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/8/29.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "RunTimeVC.h"
#import <objc/runtime.h>
#import "RTPerson.h"
#import "UIView+Awesome.h"
@interface RunTimeVC ()
@property (nonatomic,strong) RTPerson *person;
@end

@implementation RunTimeVC
//- (void)viewDidAppear:(BOOL)animated{
//    [super viewDidAppear:animated];
//    
//    NSLog(@"哈哈哈哈");
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.person = [[RTPerson alloc]init];
    self.person.name = @"alex";
}

- (void)Method{
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self getVar];
//    [self getMethod];
//    [self changeIVarValue];
//    [self addMethod];
//    [self exchangeMethod];
    [self addVar];
}
#pragma mark 获取属性/成员变量
- (void)getVar{
    unsigned int count = 0;
    unsigned int pCount = 0;
    Ivar *allIvarList = class_copyIvarList([RTPerson class], &count);
    for (NSInteger i = 0; i < count; i++) {
        Ivar ivar= allIvarList[i];
        const char*name = ivar_getName(ivar);
        const char*type = ivar_getTypeEncoding(ivar);
        NSLog(@"ivar->%s==%s",name,type);
    }
    
    objc_property_t *allPropertyList = class_copyPropertyList([RTPerson class], &pCount);
    for (NSInteger i = 0; i < pCount; i++) {
        objc_property_t objc = allPropertyList[i];
        const char*name = property_getName(objc);
        NSLog(@"property->%s",name);
    }
}

#pragma mark 获取函数
- (void)getMethod{
    unsigned int methodCount = 0;
    Method *methodList = class_copyMethodList([RTPerson class], &methodCount);
    for (NSInteger i = 0; i < methodCount; i++) {
        Method method = methodList[i];
        //获取SEL：SEL类型,即获取方法选择器@selector()
        SEL sel = method_getName(method);
        //得到sel的方法名：以字符串格式获取sel的name，也即@selector()中的方法名称
        const char *methodName = sel_getName(sel);
        NSLog(@"method->%s",methodName);
    }
}

#pragma mark 改变Person变量的数值
-(void)changeIVarValue{
    NSLog(@"修改前: %@",_person.name);
    unsigned int count = 0;
    Ivar *allIvarList = class_copyIvarList([RTPerson class], &count);
    for (NSInteger i = 0; i < count; i++) {
        Ivar var = allIvarList[i];
        const char *varName = ivar_getName(var);
        NSString *name = [NSString stringWithUTF8String:varName];
        if ([name isEqualToString:@"_name"]) {
            object_setIvar(_person, var, @"mahong");
        }
    }
    NSLog(@"修改后:%@",_person.name);
}
#pragma mark 动态添加方法
-(void)addMethod{
    class_addMethod([RTPerson class], @selector(methodAddTest), (IMP)methodAddTest, "v@:");
    if ([_person respondsToSelector:@selector(methodAddTest)]) {
        [_person performSelector:@selector(methodAddTest)];
    }else{
        NSLog(@"添加方法失败");
    }
}
void methodAddTest(id self,SEL _cmd){
    NSLog(@"添加成功%s",__func__);
}
#pragma mark 动态交换方法
-(void)exchangeMethod{
    Method m1 = class_getInstanceMethod([RTPerson class], @selector(method1));
    Method m2 = class_getInstanceMethod([RTPerson class], @selector(method3));
    method_exchangeImplementations(m1, m2);
    [_person method3];
}
#pragma mark 给分类增加属性
- (void)addVar{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
    view.backgroundColor = [UIColor redColor];
    [view lt_setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:view];
}
@end
